package com.mianyang.zhangsan.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mianyang.zhangsan.common.base.BaseController;
import com.mianyang.zhangsan.common.base.R;
import com.mianyang.zhangsan.model.dto.SecRoleDto;
import com.mianyang.zhangsan.model.entity.SecAuthority;
import com.mianyang.zhangsan.model.entity.SecRole;
import com.mianyang.zhangsan.service.ISecRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiangjr
 * @since 2022-10-23
 */
@RestController
@RequestMapping("/role")
public class SecRoleController extends BaseController {
    @Autowired
    private ISecRoleService roleService;

    /**
     * 新增角色
     */
    @PostMapping("/save")
    public R saveRole(@RequestBody SecRole role) {
        roleService.save(role);
        return success();
    }

    /**
     * 修改角色
     */
    @PutMapping("/edit")
    public R editRole(@RequestBody SecRole role) {
        roleService.updateById(role);
        return success();
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/delete/{roleId}")
    public R deleteRole(@PathVariable("roleId") Long roleId){
        roleService.removeById(roleId);
        return success();
    }

    /**
     * 查询角色
     */
    @GetMapping("/query")
    public R searchRoles(@RequestParam(value = "roleName", required = false) String roleName,
                         @RequestParam(value = "current", defaultValue = "1") Integer current,
                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        Page<SecRole> roles = roleService.selectRoles(roleName, current, pageSize);
        return success("roles", roles);
    }

    /**
     * 查询角色权限
     */
    @GetMapping("/query/{roleId}")
    public R searchRoleAuth(@PathVariable("roleId") Long roleId) {
        List<Long> roleIds = new  ArrayList<>();
        roleIds.add(roleId);
        List<SecAuthority> authorities = roleService.selectAuthorityByRoles(roleIds);
        return success("authorities", authorities);
    }

}
